using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SqlServer.ReportingServices2005;
using NAnt.Core;
using NAnt.Core.Attributes;
/*;
 				<dataSource name="IVDBData"
							connectionString=""
							extension=""
							authentication="integrated|stored">
					<credentials windowsAccount="true"
								 username=""
								 password=""
								 impersonate="true" />
								 
				</dataSource>

 */
namespace DeployReports.Types
{
	public enum ConnectionType
	{
		SqlServer, AnalysisServices, OLEDB
	}

	[ElementName("dataSource")]
	public class DataSourceElement : Element
	{
		private string dataSourceName;
		private string connectionString;
		private ConnectionType connectionType;
		private CredentialRetrievalEnum credentialRetrieval;
		private AuthenticationElement authenticationElement;

		[TaskAttribute("connectionString", Required = true)]
		public string ConnectionString
		{
			get { return connectionString; }
			set { connectionString = value; }
		}

		[TaskAttribute("connectionType", Required = true)]
		public ConnectionType ConnectionType
		{
			get { return connectionType; }
			set { connectionType = value; }
		}

		[TaskAttribute("credentialRetrieval", Required = true)]
		public CredentialRetrievalEnum CredentialRetrieval
		{
			get { return credentialRetrieval; }
			set { credentialRetrieval = value; }
		}

		[TaskAttribute("dataSourceName", Required = true)]
		public string DataSourceName
		{
			get { return dataSourceName; }
			set { dataSourceName = value; }
		}

		[BuildElement("authentication", Required = false)]
		public AuthenticationElement AuthenticationElement
		{
			get { return authenticationElement; }
			set { authenticationElement = value; }
		}

		public DataSourceDefinition GetDataSourceDefinition()
		{
			DataSourceDefinition def = new DataSourceDefinition();

			def.ConnectString = ConnectionString;
			def.Enabled = true;
			def.EnabledSpecified = true;
			def.Extension = GetExtension(ConnectionType);
			def.CredentialRetrieval = CredentialRetrieval;
			if (CredentialRetrieval == CredentialRetrievalEnum.Store)
			{
				def.WindowsCredentials = AuthenticationElement.WindowsAccount;
				def.UserName = AuthenticationElement.UserName;
				def.Password = AuthenticationElement.Password;
			}
			else
			{
				def.WindowsCredentials = (CredentialRetrieval == CredentialRetrievalEnum.Integrated);
			}
			def.ImpersonateUserSpecified = true;
			return def;
		}

		private static string GetExtension(ConnectionType connectionType)
		{
			switch (connectionType)
			{
				case Types.ConnectionType.AnalysisServices:
					return "OLEDB-MD";
				case Types.ConnectionType.OLEDB:
					return "OLEDB";
				default:
					return "SQL";
			}
		}


		///<summary>
		///
		///            Derived classes should override to this method to provide extra 
		///            initialization and validation not covered by the base class.
		///            
		///</summary>
		///
		///<param name="elementNode">The XML node of the element to use for initialization.</param>
		protected override void InitializeElement(XmlNode elementNode)
		{
			base.InitializeElement(elementNode);
			if (CredentialRetrieval==CredentialRetrievalEnum.Store)
			{
				if (AuthenticationElement==null)
				{
					throw new BuildException(
						"Authentication element must be given for a data source that stores credentials on the server.");
				}
			}
		}
	}
}
